import scrapy

from ScrapyObject.spiders.utils.url_utils import *

'''
scrapy crawl ydax -o ydax.json
https://dhe-woojeakeew.chuvvip7y8m2.net/hflvvip/
'''


class YdaxSpider(scrapy.Spider):
    name = "ydax"
    # 前缀
    prefix = 'https://dhe-woojeakeew.'
    # 中缀
    website = 'chuvvip7y8m2'
    # 后缀
    suffix = '.net/'
    allowed_domains = [website + '.net']
    start_urls = [prefix + website + suffix + 'hflvvip/']

    def __init__(self):
        self.i = 0

    def parse(self, response):
        content = get_data(response)
        urls = response.xpath("//div[@class='detail-poster']//a/@ href").extract()
        picture_urls = response.xpath("//div[@class='detail-poster']//a//img/@ src").extract()
        tags = response.xpath("//div[@class='breadcrumbs']//a/text()").extract()
        name = response.xpath("//div[@class='breadcrumbs']//span/text()").extract()
        if len(urls) and len(picture_urls) and len(tags) and len(name):
            self.i = self.i + 1
            yield get_video_item(id=self.i, tags=tags[-1], url=urljoin(self.prefix + self.website + self.suffix, urls[0]), name=name[0], pUrl=picture_urls[0], vUrl='')
        video_urls = get_video_url_three(content)
        if len(video_urls):
            self.i = self.i + 1
            yield get_video_item(id=self.i, url=response.url, name='', pUrl='', vUrl=format_url_one(video_urls[0]))
        url_list = get_url(content)
        for url in url_list:
            if url.startswith('/'):
                yield scrapy.Request(split_joint(self.prefix + self.website + self.suffix, url), callback=self.parse)
